﻿using System;
using System.Configuration;
using System.Diagnostics;
using System.ServiceProcess;
using Sustainalytics.Utils;

namespace Sustainalytics.Admin.CRMSynchronization.Service
{
    public partial class CRMSynchronizationService : ServiceBase
    {
        public CRMSynchronizationService()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            try
            {
                var hostUrl = ConfigurationManager.AppSettings["HostUrl"];

                EventLog.WriteEntry(this.ServiceName, "Sustainalytics.Admin.CRMSynchronization.Service starting on " + hostUrl + "...", EventLogEntryType.Information);

                Microsoft.Owin.Hosting.WebApp.Start<Startup>(hostUrl);

                EventLog.WriteEntry(this.ServiceName, "Sustainalytics.Admin.CRMSynchronization.Service running on " + hostUrl + "...", EventLogEntryType.Information);
            }
            catch (Exception ex)
            {
                EventLog.WriteEntry(this.ServiceName, "OnStart error: " + Environment.NewLine + ExceptionUtility.GetInfo(ex, true), EventLogEntryType.Error);
                throw;
            }
        }

        protected override void OnStop()
        {
        }
    }
}
